1838D - Bracket Walk - CodeForces Solution


constructive algorithms data structures strings

Please click on ads to support us..

C++ Code:

#include <set>
#include <iostream>
#include <vector>

using namespace std;


int main() {
    int n, q; cin >> n >> q;
    vector<int> brackets;
    for (int i=0;i<n;i++) {
        char b; cin >> b;
        if (b == '(') brackets.push_back(0);
        else brackets.push_back(1);
    }

    set<int> open_on_odd;
    set<int> closed_on_even;
    for (int i=0;i<n;i++) {
        if (i % 2 == 0 && brackets[i] == 1) closed_on_even.insert(i);
        if (i % 2 == 1 && brackets[i] == 0) open_on_odd.insert(i);
    }

    for (int i=0;i<q;i++) {
        int x; cin >> x;
        x--;
        if (x % 2 == 0){
            if (closed_on_even.find(x) != closed_on_even.end()) {
                closed_on_even.erase(x);
            } else {
                closed_on_even.insert(x);
            }
        } else {
            if (open_on_odd.find(x) != open_on_odd.end()) {
                open_on_odd.erase(x);
            } else {
                open_on_odd.insert(x);
            }
        }

        if (n % 2 != 0) {
            cout << "NO" << endl;
            continue;
        }
        if (open_on_odd.empty() != closed_on_even.empty()) {
            cout << "NO" << endl;
            continue;
        }
        if (open_on_odd.empty() && closed_on_even.empty()) {
            cout << "YES" << endl;
            continue;
        }

        int min_op = *open_on_odd.begin();
        int max_op = *open_on_odd.rbegin();
        int min_cl = *closed_on_even.begin();
        int max_cl = *closed_on_even.rbegin();

        if (min_op < min_cl && max_op < max_cl) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
}


Comments

Submit
0 Comments
More Questions

1529B - Sifid and Strange Subsequences
1455C - Ping-pong
1644C - Increase Subarray Sums
1433A - Boring Apartments
1428B - Belted Rooms
519B - A and B and Compilation Errors
1152B - Neko Performs Cat Furrier Transform
1411A - In-game Chat
119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon